// modules =================================================
var express        = require('express');
var app            = express();
var mongoose       = require('mongoose');
var bodyParser     = require('body-parser');
var methodOverride = require('method-override');
var connectionPool = require('express-myconnection');
var fileUpload     = require('express-fileupload');
var http           = require("http");
var webSocketServer = require("websocket").server;
var callfile       = require('child_process');
// var path           = require('path');
// configuration ===========================================
	
// config files
// var db = require('./config/db');
// if(db){
//   console.log("success to connect the database!");
// }

var port = process.env.PORT || 3000; // set our port
// mongoose.connect(db.url); // connect to our mongoDB database (commented out after you enter in your own credentials)

const server = http.createServer(app);
var mysql = require("mysql");

var dbOptions ={
	connectionLimit:20,
	host : '192.168.1.16',
	user:'root',
	password:'cuc1q2w3e4r',
	port:3306,	
	database:'SecurityCheck',
	multipleStatements: true //多条sql语句执行
};

var pool = connectionPool(mysql,dbOptions,'pool');
app.use(pool);
console.log(pool);


// connectionPool.on('connection',function(connection){
	
// })
// connection.connect(function(err){
// 	if(err){
// 		console.log('error connecting: '+ err.stack);
// 		return;
// 	}
// 	console.log('connected as id' +connection.threadId);
// });

// get all data/stuff of the body (POST) parameters
app.use(bodyParser.json()); // parse application/json 
app.use(bodyParser.json({ type: 'application/vnd.api+json' })); // parse application/vnd.api+json as json
app.use(bodyParser.urlencoded({ extended: true })); // parse application/x-www-form-urlencoded

app.use(methodOverride('X-HTTP-Method-Override')); // override with the X-HTTP-Method-Override header in the request. simulate DELETE/PUT
app.use(express.static(__dirname + '/public')); // set the static files location /public/img will be /img for users
app.use(fileUpload());
// routes ==================================================
require('./app/Securitysystem.server.routes')(app); // pass our application into our routes
// start app ===============================================
server.listen(port);	
console.log('Magic happens on port ' + port); 			// shoutout to the user

exports = module.exports = app; 						// expose app
